home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / Miscellaneous / MPW p2c / p2cLibraries / PStrings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-29  |  5.7 KB  |  211 lines  |  [TEXT/MPS ]

  1. /*---------------------------------------------------------------------------*
  2.  |                                                                           |
  3.  |                <<< PStrings.c - Pascal string routines >>>                |
  4.  |                                                                           |
  5.  |                   Copyright Apple Computer, Inc. 1994-5                     |
  6.  |                            All rights reserved.                           |
  7.  |                                                                           |
  8.  *---------------------------------------------------------------------------*/
  9.  
  10. /* 
  11.  
  12. This file defines the following routines:
  13.  
  14. PStr_Set(dst, src)                             - Assign one Pascal string to another
  15. PStr_Cmp(s1, s2)                                - Compare two Pascal strings
  16. PStr_Pos(sub, src)                            - Position from left of substring in source string
  17. PStr_Copy(dst, src, amount)            -    Copy src string chars to dst and form a string
  18. PStr_Insert(dst, ins, start)        - Insert a string into another
  19. PStr_Delete(dst, start, amount)    - Delete characters from a string
  20. PStr_Concat(dst, s1,...,NULL)        -    Concat arbitrary number of strings
  21.     
  22. */
  23.  
  24. #include <Types.h>
  25. #include <CType.h>
  26. #include <String.h>
  27. #include <StdArg.h>
  28.  
  29.  
  30. /*------------------------------------------------*
  31.  | PStr_Set - Assign one Pascal string to another |
  32.  *------------------------------------------------*
  33.  
  34.      Assigns value of second string to first.
  35. */
  36.  
  37. void PStr_Set(unsigned char *destS, unsigned char *srcS)
  38. {
  39.     memcpy(destS, srcS, *srcS + 1);                                /* + 1 for length byte */
  40. }
  41.  
  42.  
  43. /*---------------------------------------*
  44.  | PStr_Cmp - Compare two Pascal strings |
  45.  *---------------------------------------*
  46.  
  47.  Compares the contents of two Pascal strings and returns <0 if s1 < s2, 0 if
  48.  s1 == s2, and >0 if s1 > s2.
  49. */
  50.  
  51. short PStr_Cmp(unsigned char *s1, unsigned char *s2)
  52. {
  53.     register int n = (unsigned char)*s1++;
  54.     int m = (unsigned char)*s2++;
  55.     
  56.     if ((m = n - m) != 0) return (m);
  57.     
  58.     for (; n && *s1 == *s2; n--, s1++, s2++);
  59.     return (n ? *s1 - *s2 : 0);
  60. }
  61.  
  62.  
  63. /*-----------------------------------------------*
  64.  | PStr_Pos - Find 1st occurrence of a substring |
  65.  *-----------------------------------------------*
  66.  
  67.  Find the first occurrence of Pascal string sub in the Pascal string src and
  68.     return the index of the first occurrence.
  69. */
  70.  
  71. short PStr_Pos(unsigned char *sub, unsigned char *src)
  72. {
  73.         register int sublen, limit;
  74.         register unsigned char *p, *q, n;
  75.         unsigned char *origSrc = src;
  76.     
  77.         sublen = (unsigned char)*sub++;
  78.         if (sublen == 0) return (0);
  79.         
  80.         limit = (unsigned char)*src++ - sublen;
  81.         if (limit < 0) return (0);
  82.         
  83.         while (limit-- >= 0) {
  84.             q = src;
  85.             p = sub;
  86.             n = sublen;
  87.             while (n--) if (*p++ != *q++) goto next;
  88.             return ((short)(src - origSrc));
  89. next: src++; 
  90.         }
  91.         return (0);
  92. }
  93.  
  94.  
  95. /*--------------------------------------------------*
  96.  | PStr_Copy - Copy a (sub)string to another string |
  97.  *--------------------------------------------------*
  98.  
  99.     Copy amount characters (or up to the last character of the src Pascal string)
  100.     from the src Pascal string starting at startPos in src to the dst Pascal string
  101.     and return a pointer to the dst Pascal string.  It is up to the caller to make
  102.     sure enough space is reserved in the dst string.
  103. */
  104.  
  105. unsigned char *PStr_Copy(unsigned char *dst, unsigned char *src,
  106.                                                  int startPos, int amount)
  107. {
  108.     unsigned char *start = src + startPos;
  109.     unsigned char *p = dst++;
  110.     unsigned char *q = src + *src + 1;
  111.  
  112.     if (start >= q) {
  113.         *p = '\0';
  114.         return(p);
  115.     }
  116.     
  117.     if (start+amount > q)
  118.         amount = q - start;
  119.     
  120.     while (amount--)
  121.         *dst++ = *start++;
  122.     *p = dst - p - 1;
  123.     return (p);
  124. }
  125.  
  126.  
  127. /*----------------------------------------------*
  128.  | PStr_Insert - Insert one string into another |
  129.  *----------------------------------------------*
  130.  
  131.  Insert the ins Pascal string into the dst string starting at position
  132.  startPos in the dst string.  The function returns original dst
  133.  Pascal string with the insert as its value.  It is up to the caller to make
  134.  sure enough space is reserved in the dst string for the insertion.
  135. */
  136.  
  137. unsigned char *PStr_Insert(unsigned char *dst, unsigned char *ins,
  138.                                                     int startPos)
  139. {
  140.     unsigned char *start = dst + startPos;
  141.     register unsigned char *q, *p = dst + (unsigned char)*dst + 1;
  142.     register int inslen;
  143.  
  144.     *dst += inslen = (unsigned char)*ins;
  145.     
  146.     q = p + inslen;
  147.     while (--p >= start) *--q = *p;
  148.     
  149.     while (inslen--) *++p = *++ins;
  150.     return (dst);
  151. }
  152.  
  153.  
  154. /*-----------------------------------------------*
  155.  | PStr_Delete - Delete characters from a string |
  156.  *-----------------------------------------------*
  157.  
  158.  Delete amount characters (or all remaining characters) from the Pascal string
  159.  starting at the character indicated by startPos.
  160. */
  161.  
  162. unsigned char *PStr_Delete(unsigned char *dst, int startPos, int amount)
  163. {
  164.     unsigned char *start = dst + startPos;
  165.     register unsigned char *p;
  166.     register int n;
  167.     
  168.     if (amount <= 0) return (dst);
  169.     
  170.     p = start + amount;
  171.     n = dst + (unsigned char)*dst - p + 1;
  172.     
  173.     if (n <= 0) {
  174.         *dst = start - dst - 1;
  175.     } else {
  176.         while (n--) *start++ = *p++;
  177.         *dst -= amount;
  178.     }
  179.  
  180.     return (dst);
  181. }
  182.  
  183.  
  184. /*-----------------------------------*
  185.  | PStr_Concat - Concatenate strings |
  186.  *-----------------------------------*
  187.  
  188.  Concatenate all the Pascal strings specified in the argument list together
  189.  with the dst Pascal string and return a pointer to the dst string.  It is up
  190.  to the caller to make sure enough space is reserved in the dst string for all
  191.  the concatenations.    No check is made to see that the string length exceeds
  192.  255 characters.
  193. */
  194.  
  195. unsigned char *PStr_Concat(unsigned char *dst, unsigned char *src1, ...)
  196. {
  197.     va_list ap;
  198.     register unsigned char n, *p, *q = dst + 1;
  199.     
  200.     *dst = 0;
  201.     
  202.     va_start(ap, dst);
  203.     
  204.     while (p = va_arg(ap, unsigned char *)) 
  205.         for (*dst += (n = (unsigned char)*p); n; n--) 
  206.             *q++ = *++p;
  207.             
  208.     va_end(ap);
  209.     return (dst);
  210. }
  211.